// pages/member/rankingListSecond/rankingListSecond.js
var app = getApp();
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    name:'',
    tabs: [],
    selected: 0,
    tabID: 0,
    bossList: [],
    detail: {},
    uid: 0,
    is_customer_uid:0,
    // more
    isLoadMore: true,
    page: 1,
    total_page: 0,
    searchMember:"",
    isSearch:false,//是否点击了搜索
    team_dividend: ''//经销商自定义名称
  },
  /**
	 * 搜索会员信息
	 */
  searchMemberval({detail}){
    this.getData({searchMember:detail,isSearch:true});
  },
  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    this.getTab();
  },

  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function() {},

  /**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function() {},

  /**
	 * 生命周期函数--监听页面隐藏
	 */
  onHide: function() {},

  /**
	 * 生命周期函数--监听页面卸载
	 */
  onUnload: function() {},

  /**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh: function() {},

  /**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function() {
    // console.log(this.data.total_page)
    if(this.data.isLoadMore == true){
      console.log('进');
      if(this.data.page < this.data.total_page){
        this.setData({
          page:this.data.page + 1,
          isLoadMore:true
        });
        wx.showLoading({
          title: '加载中',
        });
        this.getMore();
      } else if(this.data.page >= this.data.total_page){
        // console.log(this.data.page, this.data.total_page)
        this.setData({
          isLoadMore:false
        });
      }
    }
  },

  /**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function() {},

  showDetail(obj) {
    let item = obj.target.dataset.item || obj.currentTarget.dataset.item;
    if (item.show && this.data.uid === item.has_one_member.uid) {
      let arr = this.data.bossList;
      for (var i = 0; i < arr.length; i++) {
        if (arr[i].id == item.id) {
          arr[i].show = false;
        }
      }
      this.setData({
        bossList: arr
      });
      return;
    }
    this.setData({
      uid: item.has_one_member.uid
    });
    let urlStr = app.getNetAddresss('plugin.team-dividend.api.team.getInfo');
    urlStr += '&uid=' + item.has_one_member.uid;

    app._getNetWork({
      url: urlStr,
      success: (res) => {
        let resData = res.data;
        if (resData.result === 1) {
          this.setData({
            detail: resData.data
          });
          let bossArr = this.data.bossList;
          for (var i = 0; i < bossArr.length; i++) {
            if (bossArr[i].id == item.id) {
              bossArr[i].show = true;
            }
          }
          this.setData({
            bossList: bossArr
          });
        } else {
          wx.showToast({
            title: resData.msg,
            icon: 'none'
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  getTab() {
    let urlStr = app.getNetAddresss('plugin.team-dividend.api.team.getColumns');
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        let resData = res.data;
        if (resData.result === 1) {
          this.setData({
            tabs: resData.data.columns,
            tabID: resData.data.columns[0].id
          });
          this.getData();
          try {
            var value = wx.getStorageSync('mailLanguage');
            if (value) {
              let mailLanguage = JSON.parse(value);
              this.setData({
                name: mailLanguage.team_dividend && mailLanguage.team_dividend.my_agent ? mailLanguage.team_dividend.my_agent : '我的客户',
                team_dividend: mailLanguage.team_dividend && mailLanguage.team_dividend.title ? mailLanguage.team_dividend.title : '经销商'
              });
              wx.setNavigationBarTitle({
                title: this.data.name,
              });
            }
          } catch (e) {
            // Do something when catch error
          }
        } else {
          wx.showToast({
            title: resData.msg,
            icon: 'none'
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  changeTab(tab) {
    let tabs = tab.target.dataset.item || tab.currentTarget.dataset.item;
    let is = tab.target.dataset.index || tab.currentTarget.dataset.index;
    this.setData({
      selected: is,
      tabID: tabs.id
    });
    this.getData();
  },
  // 获取数据
  getData(member_info) {
    this.setData({
      page: 1,
      isLoadMore: false
    });
    let urlStr = app.getNetAddresss('plugin.team-dividend.api.team.getList');
    urlStr += '&id=' + this.data.tabID;
    if(member_info){
      urlStr += "&member_info=" + member_info;
    }
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        let resData = res.data;
        if (resData.result === 1) {
          this.setData({
            bossList: resData.data.list.data,
            isLoadMore: true,
            total_page: resData.data.list.last_page,
            is_customer_uid:resData.data.is_customer_uid == "1"?true:false,
          });
        } else {
          wx.showToast({
            title: resData.msg,
            icon: 'none'
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  getMore(){

    this.setData({
      page: this.data.page
    });
    let urlStr = app.getNetAddresss('plugin.team-dividend.api.team.getList');
    urlStr += '&id=' + this.data.tabID;
    if(this.data.isSearch){
      urlStr += "&member_info" + this.data.searchMember;
    }
    app._getNetWork({
      url: urlStr,
      data:{
        page:this.data.page
      },
      success: (res) => {
        let resData = res.data;
        if (resData.result === 1) {
          wx.hideLoading();
          this.setData({
            bossList: this.data.bossList.concat(resData.data.list.data),
            isLoadMore: true,
            total_page: resData.data.list.last_page
          });
        } else {
          wx.showToast({
            title: resData.msg,
            icon: 'none'
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  }
});
